{block name=body}

<div class="row">
	<div class="col-sm-12">
		<form class="well form-horizontal" id="Edit_Record" accept-charset="UTF-8" method="post">
			<div class="row">
				<div class="col-sm-12 edit-page-header">
					<span class="edit-page-title">{if $fields.location_id neq ""}编辑{else}新建{/if} {$zone}</span>
							{if $cnt eq ""}{assign var=cnt value=0}{/if} 
							<a href="javascript:void(0);" onClick="$('#Edit_Record').submit();" class="btn btn-primary pull-right top-btn"><span class="glyphicon glyphicon-floppy-saved"></span> 保存</a>
							<div class="published" {if $fields.location_published eq 0}style="display:none;"{/if}>
								<!-- 已发布 -->
								<a href="javascript:void(0);" onClick="saveDraft('field[1][tbl_location][{$cnt}][id]','location_object_id','location_published','field[1][tbl_location][{$cnt}][location_deleted]',false);" class="btn btn-info pull-right top-btn published"><span class="glyphicon glyphicon-floppy-disk"></span> 保存草稿</a>
								<a href="javascript:void(0);" onClick="unpublish('location_published');" class="btn btn-warning pull-right top-btn"><span class="glyphicon glyphicon-thumbs-down"></span> 不发布</a>
							</div>
							<div class="drafts" {if $fields.location_published eq 1}style="display:none;"{/if}>
								<!-- 草稿 -->
								<a href="javascript:void(0);" onClick="publish('field[1][tbl_location][{$cnt}][id]','location_object_id','location_published','field[1][tbl_location][{$cnt}][location_deleted]');" class="btn btn-primary pull-right top-btn drafts"><span class="glyphicon glyphicon-thumbs-up"></span> 保存 &amp; 发布</a>
							</div>
					<input type="hidden" value="location_id" name="primary_id" id="primary_id"/> 
					<input type="hidden" value="location_id" name="field[1][tbl_location][{$cnt}][id]" id="id"/> 
					<input type="hidden" value="{$fields.location_id}" name="field[1][tbl_location][{$cnt}][location_id]" id="location_id" class="key">
					<input type="hidden" value="{$rootParentID}" name="field[1][tbl_location][{$cnt}][location_listing_id]" id="location_listing_id">
					<input type="hidden" value="{if $fields.location_object_id}{$fields.location_object_id}{else}{$objID}{/if}" name="field[1][tbl_location][{$cnt}][location_object_id]" id="location_object_id">
					<input type="hidden" value="{if $fields.location_created}{$fields.location_created}{else}{'Y-m-d H:i:s'|date}{/if}" name="field[1][tbl_location][{$cnt}][location_created]" id="location_created">
					<input type="hidden" value="{$fields.location_published}" name="field[1][tbl_location][{$cnt}][location_published]" id="location_published">
					<input type="hidden" name="formToken" id="formToken" value="{$token}" /> 
				</div>
			</div>
			<ul class="nav nav-tabs" id="myTab">
				<li class="active"><a href="#details" data-toggle="tab">详情</a></li>
				<li><a href="#log" data-toggle="tab">日志</a></li>
			</ul>
		
			<div class="tab-content">
				<!--===+++===+++===+++===+++===+++ DETAILS TAB +++===+++===+++===+++===+++====-->
				<div class="tab-pane active" id="details">
					<div class="row form" data-error="<b>详情</b>选项上发现错误。打开<b>详情</b>选项以查看特定错误通知。">
						
						<div class="row form-group">
							<label class="col-sm-3 control-label" for="id_location_name">* 名称</label>
							<div class="col-sm-5">
								<input class="form-control" type="text" value="{$fields.location_name}" name="field[1][tbl_location][{$cnt}][location_name]" id="id_location_name" required>
								<span class="help-block"></span>
							</div>
						</div>
						<!-- <div class="row form-group">
			        <label class="col-sm-3 control-label" for="id_location_description">描述</label>
			        <div class="col-sm-5">
			          <textarea name="field[1][tbl_location][{$cnt}][location_description]" id="id_location_description" class="tinymce">{$fields.location_description}</textarea>
			        </div>
			      </div> -->
			      <div class="row form-group">
							<label class="col-sm-3 control-label" for="id_location_street">详细地址</label>
							<div class="col-sm-5">
								<input class="form-control" type="text" value="{$fields.location_street}" onchange="fillLocation();" name="field[1][tbl_location][{$cnt}][location_street]" id="id_location_street">
							</div>
						</div>
						<div class="row form-group">
							<label class="col-sm-3 control-label" for="id_location_suburb">Suburb</label>
							<div class="col-sm-5">
								<input class="form-control" type="text" value="{$fields.location_suburb}" onchange="fillLocation();" name="field[1][tbl_location][{$cnt}][location_suburb]" id="id_location_suburb">
							</div>
						</div>
						<div class="row form-group">
							<label class="col-sm-3 control-label" for="id_location_state">State</label>
							<div class="col-sm-5">
								<input class="form-control" type="text" value="{$fields.location_state}" onchange="fillLocation();" name="field[1][tbl_location][{$cnt}][location_state]" id="id_location_state">
							</div>
						</div>
						<div class="row form-group">
							<label class="col-sm-3 control-label" for="id_location_postcode">Postcode</label>
							<div class="col-sm-5">
								<input class="form-control" type="text" value="{$fields.location_postcode}" onchange="fillLocation();" name="field[1][tbl_location][{$cnt}][location_postcode]" id="id_location_postcode">
							</div>
						</div>
						<div class="row form-group">
							<label class="col-sm-3 control-label" for="id_location_country">Country</label>
							<div class="col-sm-5">
								<input class="form-control" type="text" value="{$fields.location_country}" onchange="fillLocation();" name="field[1][tbl_location][{$cnt}][location_country]" id="id_location_country">
							</div>
						</div>
						<div class="row form-group">
							<label class="col-sm-3 control-label" for="search">Location</label>
							<div class="col-sm-7">
								<input class="form-control location-input" type="text" value="" id="search">&nbsp;<a href="javascript:void(0);" class="btn btn-info" onclick="searchAddress($('#search').val());$('#search').val('')">Search</a>
								<div id="search-warning"></div>
								<input type="hidden" value="{$fields.location_latitude}" name="field[1][tbl_location][{$cnt}][location_latitude]" id="location_latitude"> 
								<input type="hidden" value="{$fields.location_longitude}" name="field[1][tbl_location][{$cnt}][location_longitude]" id="location_longitude">
								<div id="GmlMap" class="GmlMap">Loading Map....</div>
								<script type="text/javascript">
							          jQuery(document).ready(function() {
							            centerOn({$fields.location_latitude},{$fields.location_longitude});
							          });
							        </script>
								<script src="//maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script>
								<script type="text/javascript" src="/admin/includes/google-api/gml-v3.js"></script>
								<link href='/admin/includes/google-api/gml-v3.css' rel='stylesheet' type='text/css'>
							</div>
						</div>
			      <div class="row form-group">
							<label class="col-sm-3 control-label" for="id_location_phone">电话</label>
							<div class="col-sm-5">
								<input class="form-control" type="text" value="{$fields.location_phone}" name="field[1][tbl_location][{$cnt}][location_phone]" id="id_location_phone">
							</div>
						</div>
						<!-- <div class="row form-group">
							<label class="col-sm-3 control-label" for="id_location_fax">Fax</label>
							<div class="col-sm-5">
								<input class="form-control" type="text" value="{$fields.location_fax}" name="field[1][tbl_location][{$cnt}][location_fax]" id="id_location_fax">
							</div>
						</div> -->
						<div class="row form-group">
							<label class="col-sm-3 control-label" for="id_location_email">Email</label>
							<div class="col-sm-5">
								<input class="form-control" type="text" value="{$fields.location_email}" name="field[1][tbl_location][{$cnt}][location_email]" id="id_location_email">
							</div>
						</div>
						<div class="row form-group">
							<label class="col-sm-3 control-label" for="id_location_url">Website<br><small>Include 'http://' or 'https://'</small></label>
							<div class="col-sm-5">
								<input class="form-control" type="text" value="{$fields.location_url}" name="field[1][tbl_location][{$cnt}][location_url]" id="id_location_url">
							</div>
						</div>
			      <!-- <div class="row form-group">
              <label class="col-sm-3 control-label" for="id_location_enquiry_recipient">Enquiry Recipient</label>
              <div class="col-sm-5">
                <input class="form-control" type="text" value="{$fields.location_enquiry_recipient}" name="field[1][tbl_location][{$cnt}][location_enquiry_recipient]" id="id_location_enquiry_recipient">
              </div>
            </div>
            <div class="row form-group">
              <label class="col-sm-3 control-label" for="id_location_order_recipient">Order Recipient</label>
              <div class="col-sm-5">
                <input class="form-control" type="text" value="{$fields.location_order_recipient}" name="field[1][tbl_location][{$cnt}][location_order_recipient]" id="id_location_order_recipient">
              </div>
            </div>
            <div class="row form-group">
              <label class="col-sm-3 control-label" for="id_location_bcc_recipient">BCC Recipient</label>
              <div class="col-sm-5">
                <input class="form-control" type="text" value="{$fields.location_bcc_recipient}" name="field[1][tbl_location][{$cnt}][location_bcc_recipient]" id="id_location_bcc_recipient">
              </div>
            </div> 
			      <div class="row form-group">
								<label class="col-sm-3 control-label" for="location_image">Image<br>
								<small>尺寸: 350px宽 * 350px高 <br>(optional)</small></label>
							<div class="col-sm-9">
								<input type="hidden" value="{$fields.location_image}" name="field[1][tbl_location][{$cnt}][location_image]" id="location_image_link" class="fileinput"> 
								<span class="file-view" id="location_image_path"> {if $fields.location_image}<a href="{$fields.location_image}" target="_blank" >查看</a>{else}暂无{/if} </span> 
								<a href="javascript:void(0);" class="btn btn-info marg-5r" onclick="getFileType('location_image','','');">选择文件</a> 
								<a href="javascript:void(0);" class="btn btn-info" onclick="$('#location_image_link').val('');$('#location_image_path').html('None');">移除文件</a>
							</div>
						</div>-->

					</div>
				</div>
				<!--===+++===+++===+++===+++===+++ LOG TAB +++===+++===+++===+++===+++====-->
				<div class="tab-pane" id="log">
					<div class="row form" id="tags-wrapper">
						<div class="col-sm-12">
							{if $fields.logs}
								<table class="table table-bordered table-striped table-hover">
									<thead>
										<tr>
											<th>日期</td>
											<th>操作</td>
											<th>用户</td>
										</tr>
									</thead>
									<tbody>
									{foreach $fields.logs as $log}
										<tr {if $log.location_id eq $fields.location_id}class="info"{/if}>
											<td>{$log.log_created|date_format:"%d/%b/%Y %r"}</td>
											<td>{$log.log_action}{if $log.log_action eq 'Add' || $log.log_action eq 'Delete'} draft{/if}</td>
											<td>{$log.admin_name}</td>
										</tr>
									{/foreach}
									</tbody>
								</table>
							{else}
								暂无日志。
							{/if}
						</div>
					</div>
				</div>
			</div>
			
			<div class="row form-group form-bottom-btns">
				<a href="javascript:void(0);" onClick="$('#Edit_Record').submit();" class="btn btn-primary pull-right top-btn"><span class="glyphicon glyphicon-floppy-saved"></span> 保存</a>
				<div class="published" {if $fields.location_published eq 0}style="display:none;"{/if}>
					<!-- 已发布 -->
					<a href="javascript:void(0);" onClick="saveDraft('field[1][tbl_location][{$cnt}][id]','location_object_id','location_published','field[1][tbl_location][{$cnt}][location_deleted]', false);" class="btn btn-info pull-right top-btn published"><span class="glyphicon glyphicon-floppy-disk"></span> 保存草稿</a>
					<a href="javascript:void(0);" onClick="unpublish('location_published');" class="btn btn-warning pull-right top-btn"><span class="glyphicon glyphicon-thumbs-down"></span> 不发布</a>
				</div>
				<div class="drafts" {if $fields.location_published eq 1}style="display:none;"{/if}>
					<!-- 草稿 -->
					<a href="javascript:void(0);" onClick="publish('field[1][tbl_location][{$cnt}][id]','location_object_id','location_published','field[1][tbl_location][{$cnt}][location_deleted]');" class="btn btn-primary pull-right top-btn drafts"><span class="glyphicon glyphicon-thumbs-up"></span> 保存 &amp; 发布</a>
				</div>
			</div>
		</form>
	</div>
</div>

{include file='jquery-validation.tpl'}
<script type="text/javascript">

$(document).ready(function(){

	$('#Edit_Record').validate({
		onkeyup: false
	});
	
});

function saveDraft(id_name,objId_name,publish_name, field_name, preview){
	if ($('#Edit_Record').valid()) { 
		$('body').css('cursor', 'wait');
		$('#'+publish_name).val('0');
		var id_key0 = encodeURIComponent(id_name+'[0]');
		var id_key1 = encodeURIComponent(id_name+'[1]');
		var objId_key = encodeURIComponent($('#'+objId_name).attr('name'));
		var publish_key = encodeURIComponent($('#'+publish_name).attr('name'));
		var field_key = encodeURIComponent(field_name);
		var field_value = encodeURIComponent(mysql_now());
		$.ajax({
			type : "POST",
			url : "/admin/includes/processes/processes-record.php",
			cache: false,
			async: false,
			data : id_key0+'='+objId_name+'&'+id_key1+'='+publish_name+'&'+objId_key+"="+$('#'+objId_name).val()+"&"+publish_key+"=0&"+field_key+"="+field_value+'&formToken='+$('#formToken').val(),
			dataType: "html",
			success : function(data, textStatus) {
				try {
					var obj = $.parseJSON(data);
					if(obj.notice){ 
						$('.key').val('');
						$('#Edit_Record').submit();
						$('.published').hide();
						$('.drafts').show();
// 						buildUrl('tbl_location','location_parent_id',objId_name, preview);
					}
				} catch (err) {}
				$('body').css('cursor', 'default');
			}
		});
		$('body').css('cursor', 'default');
	} 
}

function publish(id_name,objId_name,publish_name,field_name){
	if ($('#Edit_Record').valid()) { 
		$('body').css('cursor', 'wait');
		$('#'+publish_name).val('1');
		var id_key0 = encodeURIComponent(id_name+'[0]');
		var id_key1 = encodeURIComponent(id_name+'[1]');
		var objId_key = encodeURIComponent($('#'+objId_name).attr('name'));
		var publish_key = encodeURIComponent($('#'+publish_name).attr('name'));
		var field_key = encodeURIComponent(field_name);
		var field_value = encodeURIComponent(mysql_now());
		$.ajax({
			type : "POST",
			url : "/admin/includes/processes/processes-record.php",
			cache: false,
			data : id_key0+'='+objId_name+'&'+id_key1+'='+publish_name+'&'+objId_key+"="+$('#'+objId_name).val()+"&"+publish_key+"=1&"+field_key+"="+field_value+'&formToken='+$('#formToken').val(),
			dataType: "html",
			success : function(data, textStatus) {
				try {
					var obj = $.parseJSON(data);
					if(obj.notice){ 
						$('#Edit_Record').submit();
						$('.drafts').hide();
						$('.published').show();
					}
				} catch (err) {}
				$('body').css('cursor', 'default');
			}
		});
		$('body').css('cursor', 'default');
	} 
}

function unpublish(publish_name){
	$('#'+publish_name).val('0');
	$('#Edit_Record').submit();
	$('.published').hide();
	$('.drafts').show();
}


function fillLocation(){
	var loc_str = $('#id_location_street').val() + ', ' + $('#id_location_suburb').val() + ', ' + $('#id_location_state').val() + ' ' + $('#id_location_postcode').val() + ' ' + $('#id_location_country').val();
	$('#search').val(loc_str); 
}
</script>
{/block}
